/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.minhphuong.controllers;

import com.minhphuong.bll.ChiTietHoaDonBLL;
import com.minhphuong.bll.ChiTietSanPhamBLL;
import com.minhphuong.bll.HangBLL;
import com.minhphuong.bll.HelperBLL;
import com.minhphuong.bll.HoaDonBLL;
import com.minhphuong.bll.MauBLL;
import com.minhphuong.bll.MauSanPhamBLL;
import com.minhphuong.bll.NguoiDungBLL;
import com.minhphuong.bll.QuyenBLL;
import com.minhphuong.bll.SanPhamBLL;
import com.minhphuong.bll.TinhTrangSanPhamBLL;
import com.minhphuong.bll.TrangThaiHoaDonBLL;
import com.minhphuong.dal.KhuyenMaiDAL;
import com.minhphuong.entities.ChiTietHoaDon;
import com.minhphuong.entities.ChiTietSanPham;
import com.minhphuong.entities.Hang;
import com.minhphuong.entities.HoaDon;
import com.minhphuong.entities.KhuyenMai;
import com.minhphuong.entities.Mau;
import com.minhphuong.entities.MauSanPham;
import com.minhphuong.entities.NguoiDung;
import com.minhphuong.entities.Quyen;
import com.minhphuong.entities.SanPham;
import com.minhphuong.entities.TinhTrangSanPham;
import com.minhphuong.entities.TrangThaiHoaDon;
import com.minhphuong.helper.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author MINHPHUONG
 */
@Controller
public class AdminController {

    @RequestMapping(value = "/admin/sanpham", method = RequestMethod.GET)
    public String SanPham(ModelMap map, @ModelAttribute("sanpham") SanPham sanpham, HttpSession session) {
        HelperBLL.LoadGiaoDien(map, session);
        List<SanPham> listSanPham = SanPhamBLL.getAllSanPham();
        map.addAttribute("listSanPham", listSanPham);
        List<Mau> listMau = MauBLL.getAllMau();
        map.addAttribute("listMau", listMau);
        List<TinhTrangSanPham> listTTSP = TinhTrangSanPhamBLL.getALLTTSP();
        map.addAttribute("listTTSP", listTTSP);
        return "admin_sanpham";
    }

    @RequestMapping(value = "/admin/editsanpham/{id}", method = RequestMethod.GET)
    public String index(ModelMap map, @PathVariable int id, HttpSession session, @ModelAttribute("sanpham") SanPham sanpham) {
        HelperBLL.LoadGiaoDien(map, session);

        SanPham sanPham = SanPhamBLL.getSanPhamByID(id);
        map.addAttribute("sanPham", sanPham);

        List<Hang> listHang = HangBLL.getAllHang();
        map.addAttribute("listHang", listHang);

        List<MauSanPham> listMauSP = MauSanPhamBLL.getMauSanPhamById(id);
        map.addAttribute("listMauSP", listMauSP);

        List<Mau> listMau = MauBLL.getAllMau();
        map.addAttribute("listMau", listMau);

        List<TinhTrangSanPham> listTTSP = TinhTrangSanPhamBLL.getALLTTSP();
        map.addAttribute("listTTSP", listTTSP);

        //sanpham.setChiTietSanPham(sanPham.getChiTietSanPham());
        return "admin_editsanpham";
    }

    @RequestMapping(value = "/admin/addsanpham", method = RequestMethod.POST)
    public String ThemSanPham(HttpSession session, ModelMap model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HelperBLL.LoadGiaoDien(model, session);
        //ChiTietSanPhamBLL.insertChiTietSanPham(sanpham.getChiTietSanPham().getHinh());

        SanPham sanPham = new SanPham();
        sanPham.setHang(HangBLL.getHangByID(Integer.parseInt(request.getParameter("hang"))));
        sanPham.setTenSanPham(request.getParameter("tensanpham"));
        sanPham.setBaoHanh(Integer.parseInt(request.getParameter("baohanh")));
        sanPham.setDonGia(Integer.parseInt(request.getParameter("dongia")));

        sanPham.setHinh("samsung-dv300f.jpg");
        sanPham.setSoLuongTon(Integer.parseInt(request.getParameter("soluongton")));
        sanPham.setTinhTrangSanPham(TinhTrangSanPhamBLL.getTinhTrangSanPhamByID(Integer.parseInt(request.getParameter("tinhtrangsanpham"))));
        //sanPham.setChiTietSanPham(ChiTietSanPhamBLL.getChiTietSanPhamByID(Integer.parseInt(request.getParameter("chitietsanpham"))));

        ChiTietSanPham ctsp = new ChiTietSanPham();
        ctsp.setHinh(request.getParameter("chitietsanpham"));
        ctsp.setMaCtsp(HelperBLL.getNextID("ChiTietSanPham", "MaCtsp"));
        sanPham.setChiTietSanPham(ctsp);
        ChiTietSanPhamBLL.insertChiTietSanPham(ctsp);
        SanPhamBLL.insertSanPham(sanPham);
        MauSanPham mausp = new MauSanPham();
        for (int i = 1; i < 11; i++) {
            if (request.getParameter("mau" + i) != null) {
                mausp.setMau(MauBLL.getMauById(Integer.parseInt(request.getParameter("mau" + i))));
                mausp.setSanPham(sanPham);
                MauSanPhamBLL.insertMauSanPham(mausp);
            }
        }
        return "admin_sanpham";
    }

    @RequestMapping(value = "/admin/nguoidung", method = RequestMethod.GET)
    public String NguoiDung(ModelMap map, @ModelAttribute("nguoidung") NguoiDung nguoidung, HttpSession session
    ) {
        HelperBLL.LoadGiaoDien(map, session);
        List<NguoiDung> listNguoiDung = NguoiDungBLL.getAllNguoiDung();
        map.addAttribute("listNguoiDung", listNguoiDung);
        List<Quyen> listQuyen = QuyenBLL.getAllQuyen();
        map.addAttribute("listQuyen", listQuyen);
        return "admin_nguoidung";
    }

    @RequestMapping(value = "/admin/addnguoidung", method = RequestMethod.POST)
    public String AddNguoiDung(@ModelAttribute("nguoidung") NguoiDung nguoidung, HttpSession session, ModelMap model
    ) {
        HelperBLL.LoadGiaoDien(model, session);
        nguoidung.setKhoa(0);
        NguoiDungBLL.insertNguoiDung(nguoidung);
        List<NguoiDung> listNguoiDung = NguoiDungBLL.getAllNguoiDung();
        model.addAttribute("listNguoiDung", listNguoiDung);
        List<Quyen> listQuyen = QuyenBLL.getAllQuyen();
        model.addAttribute("listQuyen", listQuyen);

        return "admin_nguoidung";
    }

    @RequestMapping(value = "/admin/editnguoidung/{id}", method = RequestMethod.GET)
    public String EditNguoiDung(ModelMap map, @PathVariable int id, HttpSession session, @ModelAttribute("nguoidung") NguoiDung nguoidung
    ) {
        HelperBLL.LoadGiaoDien(map, session);
        NguoiDungBLL.updateNguoiDung(nguoidung);
        List<Quyen> listQuyen = QuyenBLL.getAllQuyen();
        map.addAttribute("listQuyen", listQuyen);
        return "admin_nguoidung";
    }

    @RequestMapping(value = "/admin/hoadon", method = RequestMethod.GET)
    public String HoaDon(ModelMap map, @ModelAttribute("hoadon") HoaDon hoadon, HttpSession session
    ) {
        HelperBLL.LoadGiaoDien(map, session);
        List<SanPham> listSanPham = SanPhamBLL.getAllSanPham();
        map.addAttribute("listSanPham", listSanPham);

        List<HoaDon> listHoaDon = HoaDonBLL.getAllHoaDon();
        map.addAttribute("listHoaDon", listHoaDon);

        List<TrangThaiHoaDon> listTTHD = TrangThaiHoaDonBLL.getAllTTHD();
        map.addAttribute("listTTHD", listTTHD);

        List<TinhTrangSanPham> listTTSP = TinhTrangSanPhamBLL.getALLTTSP();
        map.addAttribute("listTTSP", listTTSP);

        return "admin_hoadon";
    }

    @RequestMapping(value = "/admin/chitiethoadon/{id}", method = RequestMethod.GET)
    public String ChiTietHoaDon(ModelMap map, @PathVariable String id, @ModelAttribute("hoadon") HoaDon hoadon, HttpSession session
    ) {
        HelperBLL.LoadGiaoDien(map, session);
        HoaDon hoaDon = HoaDonBLL.getHoaDonByID(id);
        map.addAttribute("hoaDon", hoaDon);

        List<ChiTietHoaDon> ListCTHD = ChiTietHoaDonBLL.getChiTietHoaDonByID(hoaDon.getMaHd());
        map.addAttribute("listCTHD", ListCTHD);

        List<NguoiDung> listNguoiDung = NguoiDungBLL.getAllNguoiDung();
        map.addAttribute("listNguoiDung", listNguoiDung);

        List<TrangThaiHoaDon> listTTHD = TrangThaiHoaDonBLL.getAllTTHD();
        map.addAttribute("listTTHD", listTTHD);

        return "admin_detailhoadon";
    }

    @RequestMapping(value = "/admin/updatehoadon", method = RequestMethod.POST)
    public String UpdateHoaDon(@ModelAttribute("hoadon") HoaDon hoadon, ModelMap map, HttpSession session
    ) {
        HelperBLL.LoadGiaoDien(map, session);
        hoadon.setTrangThaiHoaDon(hoadon.getTrangThaiHoaDon());

        for (int i = 0; i < hoadon.getCodeHoaDon().length(); i++) {
            Object object = hoadon.getCodeHoaDon().charAt(i);
        }
        return "index";
    }

    @RequestMapping(value = "/admin/khuyenmai", method = RequestMethod.GET)
    public String KhuyenMai(ModelMap map, HttpSession session, HttpServletRequest request) {
        HelperBLL.LoadGiaoDien(map, session);
        return "admin_khuyenmai";
    }

    @RequestMapping(value = "/admin/khuyenmai/luu", method = RequestMethod.POST)
    public String LuuKhuyenMai(ModelMap map, HttpSession session, HttpServletRequest request) {
        HelperBLL.LoadGiaoDien(map, session);

        KhuyenMai khuyenMai = KhuyenMaiDAL.getKhuyenMai();
        khuyenMai.setTinhTrangKhuyenMai(Integer.parseInt(request.getParameter("promotion_on_off")));
        try {
            khuyenMai.setNgayBatDau(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(request.getParameter("datetimebegin")));
            khuyenMai.setNgayKetThuc(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(request.getParameter("datetimeend")));
        } catch (ParseException ex) {
            Logger.getLogger(AdminController.class.getName()).log(Level.SEVERE, null, ex);
        }

        khuyenMai.setPhanTram(Integer.parseInt(request.getParameter("percentdiscount")));

        KhuyenMaiDAL.saveKhuyenMai(khuyenMai);

        return "redirect:/admin/khuyenmai/?error=0";
    }

    @RequestMapping(value = "/admin/savesanpham/{id}", method = RequestMethod.POST)
    public String index(ModelMap map, @PathVariable int id, HttpSession session, HttpServletResponse response, HttpServletRequest request) {
        HelperBLL.LoadGiaoDien(map, session);

        SanPham sanPham = SanPhamBLL.getSanPhamByID(id);
        
        sanPham.setHang(HangBLL.getHangByID(Integer.parseInt(request.getParameter("hang"))));
        sanPham.setTenSanPham(request.getParameter("tensanpham"));
        sanPham.setBaoHanh(Integer.parseInt(request.getParameter("baohanh")));
        sanPham.setDonGia(Integer.parseInt(request.getParameter("dongia")));
        sanPham.setSoLuongTon(Integer.parseInt(request.getParameter("soluongton")));
        
        sanPham.setTinhTrangSanPham(TinhTrangSanPhamBLL.getTinhTrangSanPhamByID(Integer.parseInt(request.getParameter("tinhtrangsanpham"))));
        
        // lấy ctsp
        ChiTietSanPham ctsp = sanPham.getChiTietSanPham();
        ctsp.setHinh(request.getParameter("chitietsanpham"));

        ChiTietSanPhamBLL.updateChiTietSanPham(ctsp);
        SanPhamBLL.updateSanPham(sanPham);

        return "redirect:/?close=1";
    }
}
